package com.pkk.cloud.support.admin.controller.interior.roles;

import com.github.pagehelper.PageInfo;
import com.pkk.cloud.support.admin.dto.req.roles.OauthRoleAddReqDto;
import com.pkk.cloud.support.admin.dto.req.roles.OauthRoleDelReqDto;
import com.pkk.cloud.support.admin.dto.req.roles.OauthRoleIdReqDto;
import com.pkk.cloud.support.admin.dto.req.roles.OauthRolePageQueryReqDto;
import com.pkk.cloud.support.admin.dto.req.roles.OauthRoleUpdateReqDto;
import com.pkk.cloud.support.admin.dto.resp.roles.OauthSysRoleAllRespDto;
import com.pkk.cloud.support.admin.dto.resp.roles.OauthSysRolePageRespDto;
import com.pkk.cloud.support.admin.entity.resource.PenguinSysRole;
import com.pkk.cloud.support.admin.service.PenguinSysRoleService;
import com.pkk.cloud.support.oauth2.client.aspect.annotation.PreAuth;
import com.pkk.cloud.support.oauth2.common.utils.SecurityUtils;
import com.pkk.components.rpc.request.CommonRequest;
import com.pkk.components.rpc.request.PagedQueryRequest;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 授权角色的控制器
 * @author: peikunkun
 * @create: 2020-04-02 11:11
 **/
@Slf4j
@RestController
@RequestMapping("/oauth/role")
public class OauthRoleController {

  @Resource
  private PenguinSysRoleService penguinSysRoleService;

  /**
   * @Description: 添加角色
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:57
   */
  @PreAuth
  @PostMapping("add")
  public CommonResponse<Boolean> add(@Validated @RequestBody CommonRequest<OauthRoleAddReqDto> request) {
    final PenguinSysRole role = new PenguinSysRole();
    BeanUtils.copyProperties(request.getBody(), role);
    role.setCreateTime(new Date());
    role.setCreateName(SecurityUtils.getUserId());
    return ResponseUtil.success(penguinSysRoleService.saveSysRole(role));
  }


  /**
   * @Description: 删除角色操作
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("del")
  public CommonResponse<Boolean> del(@Validated @RequestBody CommonRequest<OauthRoleDelReqDto> request) {
    final String userId = SecurityUtils.getUserId();
    return ResponseUtil.success(penguinSysRoleService.delSysRoleById(request.getBody(),userId));
  }

  /**
   * @Description: 更新用户操作
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("update")
  public CommonResponse<Boolean> update(@Validated @RequestBody CommonRequest<OauthRoleUpdateReqDto> request) {
    final PenguinSysRole role = new PenguinSysRole();
    BeanUtils.copyProperties(request.getBody(), role);
    role.setUpdateTime(new Date());
    role.setUpdateName(SecurityUtils.getUserId());
    return ResponseUtil.success(penguinSysRoleService.updateSysRoleById(role));
  }


  /**
   * @Description: 获取角色所有列表操作
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("all")
  public CommonResponse<List<OauthSysRoleAllRespDto>> all() {
    return ResponseUtil.success(penguinSysRoleService.findSysRoleList());
  }


  /**
   * @Description: 获取角色信息依据主键
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<com.pkk.cloud.support.admin.dto.resp.roles.OauthSysRolePageRespDto>
   * @Author: peikunkun
   * @Date: 2020/4/8 0008 下午 5:50
   */
  @PreAuth
  @PostMapping("findOauthRoleById")
  public CommonResponse<OauthSysRolePageRespDto> findOauthRoleById(@Validated @RequestBody CommonRequest<OauthRoleIdReqDto> request){
    return ResponseUtil.success(penguinSysRoleService.findOauthRoleById(request.getBody()));
  }



  /**
   * @Description: 分页查询角色的分页查询
   * @Param: request
   * @Author: peikunkun
   * @Date: 2020/4/2 0002 下午 2:45
   */
  @PreAuth
  @PostMapping("list")
  public CommonResponse<PageInfo<OauthSysRolePageRespDto>> list(
      @Validated @RequestBody PagedQueryRequest<OauthRolePageQueryReqDto> request) {
    return ResponseUtil.success(penguinSysRoleService.findSysRoleListPage(request.getPage(), request.getBody()));
  }
}
